﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKSump.DataDefine;

namespace YJKSump.Kit
{
	internal class PlaceSumpKit
	{
		public PlaceSumpKit(ExternalCommandData commandData)
		{
			this.app = commandData.Application.Application;
			this.uiDoc = commandData.Application.ActiveUIDocument;
			this.m_Doc = this.uiDoc.Document;
		}

		public bool Run(PlaceSumpForm form)
		{
			this.GetPlashSymbol();
			this.Addids = new List<ElementId>();
			try
			{
				for (;;)
				{
					form.HasChange = false;
					this.ResetJigSymbol(form);
					SumpInstanceData sumpData = new SumpInstanceData(form.SumpType, form.SumpRadius, form.SumpLength, form.SumpWidth, form.SumpThick, form.SumpDepth, form.SumpOffset);
					this.app.DocumentChanged += this.App_DocumentChanged;
					try
					{
						this.uiDoc.PromptForFamilyInstancePlacement(this.JigSymbol);
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
					}
					this.app.DocumentChanged -= this.App_DocumentChanged;
					this.PlaceRealSump(sumpData);
					this.Addids.Clear();
					if (!form.HasChange)
					{
						break;
					}
					if (!form.isExist || !form.HasChange)
					{
						goto IL_C6;
					}
				}
				return true;
				IL_C6:;
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private void PlaceRealSump(SumpInstanceData SumpData)
		{
			Transaction transaction = new Transaction(this.m_Doc, "放置真正的实体");
			transaction.Start();
			try
			{
				foreach (ElementId elementId in this.Addids)
				{
					LocationPoint locationPoint = this.m_Doc.GetElement(elementId).Location as LocationPoint;
					XYZ xyz = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, -30.0);
					XYZ xyz2 = new XYZ(locationPoint.Point.X, locationPoint.Point.Y, 30.0);
					Line line = Line.CreateBound(xyz, xyz2);
					FamilySymbol familySymbol = this.DicString2Symbol[SumpData.strSumpType];
					familySymbol.Activate();
					FamilyInstance familyInstance = this.m_Doc.Create.NewFamilyInstance(locationPoint.Point, familySymbol, this.m_Doc.ActiveView.GenLevel, (Autodesk.Revit.DB.Structure.StructuralType)0);
					Parameter parameter = familyInstance.GetParameter("偏移量");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(0.0);
					}
					parameter = familyInstance.GetParameter("长度");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(SumpData.dLength);
					}
					parameter = familyInstance.GetParameter("宽度");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(SumpData.dWidth);
					}
					parameter = familyInstance.GetParameter("半径");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(SumpData.dRadius);
					}
					parameter = familyInstance.GetParameter("壁厚");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(SumpData.dThickness);
					}
					parameter = familyInstance.GetParameter("深度");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(SumpData.dHeight);
					}
					parameter = familyInstance.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(SumpData.dOffset);
					}
					if (familyInstance != null)
					{
						ElementTransformUtils.RotateElement(this.m_Doc, familyInstance.Id, line, locationPoint.Rotation);
					}
					this.m_Doc.Delete(elementId);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				YJKMessageBox.Show("当前界面的参数无法正确生成集水坑");
				Transaction transaction2 = new Transaction(this.m_Doc, "删除图标实体");
				transaction2.Start();
				this.m_Doc.Delete(this.Addids);
				transaction2.Commit();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void App_DocumentChanged(object sender, DocumentChangedEventArgs e)
		{
			List<ElementId> list = e.GetAddedElementIds() as List<ElementId>;
			if (list.Count > 0)
			{
				this.Addids.AddRange(list);
				int bVk = 27;
				int dwFlags = 0;
				int dwFlags2 = 2;
				PlaceSumpKit.keybd_event((byte)bVk, 0, dwFlags, 0);
				PlaceSumpKit.keybd_event((byte)bVk, 0, dwFlags2, 0);
			}
		}

		private void ResetJigSymbol(PlaceSumpForm form)
		{
			Transaction transaction = new Transaction(this.m_Doc, "重置类型");
			transaction.Start();
			try
			{
				this.JigSymbol = this.DicString2Symbol[form.HoleKind];
				if (form.HoleKind == Type2FileInfo.strRectKind)
				{
					this.JigSymbol.GetParameter("长度").Set(form.SumpLength);
					this.JigSymbol.GetParameter("宽度").Set(form.SumpWidth);
				}
				else if (form.HoleKind == Type2FileInfo.stringCircle)
				{
					this.JigSymbol.GetParameter("半径").Set(form.SumpRadius);
				}
				this.JigSymbol.GetParameter("壁厚").Set(form.SumpThick);
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private bool GetPlashSymbol()
		{
			this.DicString2Symbol = new Dictionary<string, FamilySymbol>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_SpecialityEquipment);
			List<Element> list = filteredElementCollector.WhereElementIsElementType().ToElements() as List<Element>;
			using (Dictionary<string, string>.Enumerator enumerator = Type2FileInfo.m_DicTypeToPath.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, string> pair = enumerator.Current;
					Element element = list.Find((Element s) => (s as FamilySymbol).Family.Name == pair.Value);
					if (element == null)
					{
						return false;
					}
					this.DicString2Symbol.Add(pair.Key, element as FamilySymbol);
				}
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector2.OfCategory(BuiltInCategory.OST_DetailComponents);
			list = (filteredElementCollector2.WhereElementIsElementType().ToElements() as List<Element>);
			using (Dictionary<string, string>.Enumerator enumerator = Type2FileInfo.m_DicKindToPath.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, string> pair = enumerator.Current;
					Element element2 = list.Find((Element s) => s is FamilySymbol && (s as FamilySymbol).Family != null && (s as FamilySymbol).Family.Name == pair.Value);
					if (element2 == null)
					{
						return false;
					}
					this.DicString2Symbol.Add(pair.Key, element2 as FamilySymbol);
				}
			}
			return true;
		}

		private UIDocument uiDoc;

		private Document m_Doc;

		private Application app;

		private Dictionary<string, FamilySymbol> DicString2Symbol;

		private FamilySymbol JigSymbol;

		private List<ElementId> Addids;
	}
}
